An Immutable String
Because String
s are immutable,
you can safely give this answer even though you don't know
what mysteryMethod()
does.
Once a String
has been constructed,
you know what is in it.
charAt()
The charAt(int index)
method of String
returns the character at the specified index.
Index 0 is the first character of the String
,
and the last character is at index length()-1
.
For example,
String eg = "An Immutable String"; char ch0 = eg.charAt(0); // ch0 gets 'A' char ch1 = eg.charAt(1); // ch1 gets 'n' char ch5 = eg.charAt(5); // ch5 gets 'm' char ch18 = eg.charAt( eg.length()-1 ); // ch18 gets 'g' char ch17 = eg.charAt( eg.length()-2 ); // ch17 gets 'n'
Notice that the method returns a value of type char
.
(Design Question: ) How could you make a string that contains the characters of another string in reverse order?